﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Mvc.StarterSite.Controllers;
using MvcEngine.Mvc.Models.ViewModels;
using System.Web.Mvc;
using MvcEngine.Core;
using MvcEngine.Core.Common.Slugs;
using MvcEngine.Core.Repositories;
using System.Web;
using MvcEngine.Core.Helpers;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Mvc;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc.Controllers;
using MvcEngine.Gallery.Models;
using MvcEngine.Gallery.Managers;

namespace MvcEngine.Gallery.BackOffice.Controllers
{
    public class GalleryController : BaseController
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public MvcEngineModel List()
        {
            var galleries = GalleryManager.Current.GetGalleries();
            var result = new List<GalleryDetailsViewModel>();

            foreach (var item in galleries)
            {
                var model = new GalleryDetailsViewModel()
                {
                    Name = item.Name,
                    Id = item.Id
                };
                result.Add(model);
            }
            return new MvcEngineModelList<GalleryDetailsViewModel>() { List = result, ViewName = "List" };
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public MvcEngineModel Settings(Guid galleryId)
        {
            var gallery = GalleryManager.Current.GetGallery(galleryId);

            var model = new GalleryDataSettingsViewModel()
            {
                ImagesPerPage = gallery.ImagesPerPage,
                Categories = new List<GalleryCategoryDetailsViewModel>(),
                Name = gallery.Name,
                DefaultCategory = gallery.DefaultCategoryId
            };

            var categories = GalleryCategoryManager.Current.GetAllByGallery(galleryId);

            foreach (var item in categories)
            {
                var categoryModel = new GalleryCategoryDetailsViewModel()
                {
                    Id = item.Id,
                    Name = item.Name
                };
                model.Categories.Add(categoryModel);
            }

            return model;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [AntiForgeryValidationFilter]
        [ValidateInput(false)]
        public object SettingsSave(string galleryName, GalleryData data)
        {
            if (ModelState.IsValid)
            {
                var gallery = GalleryManager.Current.GetGallery(galleryName);
                data.PageId = gallery.PageId;
                GalleryManager.Current.Save(data);
                return Json(new { model = "valid" });
            }
            return Json(new { model = "invalid", errors = ModelState.GetErrors() });
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public MvcEngineModel Install()
        {
            return new MvcEngineModel();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ActionName("Install")]
        public object InstallSave(string name, Guid pageId)
        {
            GalleryData gallery = new GalleryData() { Name = name, PageId = pageId, Id = Guid.NewGuid() };
            GalleryManager.Current.Save(gallery);
            return Text(string.Empty);
        }
    }
}
